use :: libc;
extern "C" {
    fn __cmsg_nxthdr(__mhdr: *mut msghdr, __cmsg: *mut cmsghdr) -> *mut cmsghdr;
    fn recvmsg(__fd: i32, __message: *mut msghdr, __flags: i32) -> ssize_t;
    fn __errno_location() -> *mut i32;
    fn calloc(_: u64, _: u64) -> *mut libc::c_void;
    fn free(_: *mut libc::c_void);
    fn memcpy(_: *mut libc::c_void, _: *const libc::c_void, _: u64) -> *mut libc::c_void;
    fn memmove(_: *mut libc::c_void, _: *const libc::c_void, _: u64) -> *mut libc::c_void;
    fn memset(_: *mut libc::c_void, _: i32, _: u64) -> *mut libc::c_void;
    fn close(__fd: i32) -> i32;
    fn getpid() -> __pid_t;
    fn getdtablesize() -> i32;
    fn ibuf_open(_: size_t) -> *mut ibuf;
    fn ibuf_dynamic(_: size_t, _: size_t) -> *mut ibuf;
    fn ibuf_add(_: *mut ibuf, _: *const libc::c_void, _: size_t) -> i32;
    fn ibuf_add_buf(_: *mut ibuf, _: *const ibuf) -> i32;
    fn ibuf_data(_: *const ibuf) -> *mut libc::c_void;
    fn ibuf_size(_: *const ibuf) -> size_t;
    fn ibuf_rewind(_: *mut ibuf);
    fn ibuf_close(_: *mut msgbuf, _: *mut ibuf);
    fn ibuf_get(_: *mut ibuf, _: *mut libc::c_void, _: size_t) -> i32;
    fn ibuf_get_ibuf(_: *mut ibuf, _: size_t, _: *mut ibuf) -> i32;
    fn ibuf_free(_: *mut ibuf);
    fn ibuf_fd_avail(_: *mut ibuf) -> i32;
    fn ibuf_fd_set(_: *mut ibuf, _: i32);
    fn msgbuf_init(_: *mut msgbuf);
    fn msgbuf_clear(_: *mut msgbuf);
    fn msgbuf_write(_: *mut msgbuf) -> i32;
    fn getdtablecount() -> i32;
}
pub type __uint16_t = u16;
pub type __uint32_t = u32;
pub type __pid_t = i32;
pub type __ssize_t = i64;
pub type __socklen_t = u32;
pub type pid_t = __pid_t;
pub type ssize_t = __ssize_t;
pub type size_t = u64;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct iovec {
    pub iov_base: *mut libc::c_void,
    pub iov_len: size_t,
}
pub type socklen_t = __socklen_t;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct msghdr {
    pub msg_name: *mut libc::c_void,
    pub msg_namelen: socklen_t,
    pub msg_iov: *mut iovec,
    pub msg_iovlen: size_t,
    pub msg_control: *mut libc::c_void,
    pub msg_controllen: size_t,
    pub msg_flags: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct cmsghdr {
    pub cmsg_len: size_t,
    pub cmsg_level: i32,
    pub cmsg_type: i32,
    pub __cmsg_data: [u8; 0],
}
pub type C2RustUnnamed = u32;
pub const SCM_CREDENTIALS: C2RustUnnamed = 2;
pub const SCM_RIGHTS: C2RustUnnamed = 1;
pub type uint32_t = __uint32_t;
pub type uint16_t = __uint16_t;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct ibuf {
    pub entry: C2RustUnnamed_0,
    pub buf: *mut u8,
    pub size: size_t,
    pub max: size_t,
    pub wpos: size_t,
    pub rpos: size_t,
    pub fd: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_0 {
    pub tqe_next: *mut ibuf,
    pub tqe_prev: *mut *mut ibuf,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct msgbuf {
    pub bufs: C2RustUnnamed_1,
    pub queued: uint32_t,
    pub fd: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_1 {
    pub tqh_first: *mut ibuf,
    pub tqh_last: *mut *mut ibuf,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct ibuf_read {
    pub buf: [u8; 65535],
    pub rptr: *mut u8,
    pub wpos: size_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct imsg_fd {
    pub entry: C2RustUnnamed_2,
    pub fd: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_2 {
    pub tqe_next: *mut imsg_fd,
    pub tqe_prev: *mut *mut imsg_fd,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct imsgbuf {
    pub fds: C2RustUnnamed_3,
    pub r: ibuf_read,
    pub w: msgbuf,
    pub fd: i32,
    pub pid: pid_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_3 {
    pub tqh_first: *mut imsg_fd,
    pub tqh_last: *mut *mut imsg_fd,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct imsg_hdr {
    pub type_0: uint32_t,
    pub len: uint16_t,
    pub flags: uint16_t,
    pub peerid: uint32_t,
    pub pid: uint32_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct imsg {
    pub hdr: imsg_hdr,
    pub fd: i32,
    pub data: *mut libc::c_void,
    pub buf: *mut ibuf,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union C2RustUnnamed_4 {
    pub hdr: cmsghdr,
    pub buf: [i8; 24],
}
#[no_mangle]
pub static mut imsg_fd_overhead: i32 = 0 as i32;
#[no_mangle]
pub extern "C" fn imsg_init(mut imsgbuf: *mut imsgbuf, mut fd: i32) {
    (unsafe { msgbuf_init(&mut (*imsgbuf).w) });
    (unsafe {
        memset(
            &mut (*imsgbuf).r as *mut ibuf_read as *mut libc::c_void,
            0 as i32,
            ::core::mem::size_of::<ibuf_read>() as u64,
        )
    });
    (unsafe { (*imsgbuf).fd = fd });
    (unsafe { (*imsgbuf).w.fd = fd });
    (unsafe { (*imsgbuf).pid = getpid() });
    (unsafe { (*imsgbuf).fds.tqh_first = std::ptr::null_mut::<imsg_fd>() });
    (unsafe { (*imsgbuf).fds.tqh_last = &mut (*imsgbuf).fds.tqh_first });
}
#[no_mangle]
pub extern "C" fn imsg_read(mut imsgbuf: *mut imsgbuf) -> ssize_t {
    let mut msg: msghdr = msghdr {
        msg_name: std::ptr::null_mut::<libc::c_void>(),
        msg_namelen: 0,
        msg_iov: std::ptr::null_mut::<iovec>(),
        msg_iovlen: 0,
        msg_control: std::ptr::null_mut::<libc::c_void>(),
        msg_controllen: 0,
        msg_flags: 0,
    };
    let mut cmsg: *mut cmsghdr = std::ptr::null_mut::<cmsghdr>();
    let mut cmsgbuf: C2RustUnnamed_4 = C2RustUnnamed_4 {
        hdr: cmsghdr {
            cmsg_len: 0,
            cmsg_level: 0,
            cmsg_type: 0,
            __cmsg_data: [],
        },
    };
    let mut iov: iovec = iovec {
        iov_base: std::ptr::null_mut::<libc::c_void>(),
        iov_len: 0,
    };
    let mut n: ssize_t = -(1 as i32) as ssize_t;
    let mut fd: i32 = 0;
    let mut ifd: *mut imsg_fd = std::ptr::null_mut::<imsg_fd>();
    (unsafe {
        memset(
            &mut msg as *mut msghdr as *mut libc::c_void,
            0 as i32,
            ::core::mem::size_of::<msghdr>() as u64,
        )
    });
    (unsafe {
        memset(
            &mut cmsgbuf as *mut C2RustUnnamed_4 as *mut libc::c_void,
            0 as i32,
            ::core::mem::size_of::<C2RustUnnamed_4>() as u64,
        )
    });
    iov.iov_base = (unsafe {
        ((*imsgbuf).r.buf)
            .as_mut_ptr()
            .offset((*imsgbuf).r.wpos as isize)
    }) as *mut libc::c_void;
    iov.iov_len =
        (::core::mem::size_of::<[u8; 65535]>() as u64).wrapping_sub(unsafe { (*imsgbuf).r.wpos });
    msg.msg_iov = &mut iov;
    msg.msg_iovlen = 1 as i32 as size_t;
    msg.msg_control = (unsafe { &mut cmsgbuf.buf }) as *mut [i8; 24] as *mut libc::c_void;
    msg.msg_controllen = ::core::mem::size_of::<[i8; 24]>() as u64;
    ifd = (unsafe { calloc(1 as i32 as u64, ::core::mem::size_of::<imsg_fd>() as u64) })
        as *mut imsg_fd;
    if ifd.is_null() {
        return -(1 as i32) as ssize_t;
    }
    loop {
        if (unsafe { getdtablecount() })
            + (unsafe { imsg_fd_overhead })
            + ((::core::mem::size_of::<i32>() as u64)
                .wrapping_add(::core::mem::size_of::<size_t>() as u64)
                .wrapping_sub(1 as i32 as u64)
                & !(::core::mem::size_of::<size_t>() as u64).wrapping_sub(1 as i32 as u64))
            .wrapping_add(
                (::core::mem::size_of::<cmsghdr>() as u64)
                    .wrapping_add(::core::mem::size_of::<size_t>() as u64)
                    .wrapping_sub(1 as i32 as u64)
                    & !(::core::mem::size_of::<size_t>() as u64).wrapping_sub(1 as i32 as u64),
            )
            .wrapping_sub(
                ((0 as i32 as u64)
                    .wrapping_add(::core::mem::size_of::<size_t>() as u64)
                    .wrapping_sub(1 as i32 as u64)
                    & !(::core::mem::size_of::<size_t>() as u64).wrapping_sub(1 as i32 as u64))
                .wrapping_add(
                    (::core::mem::size_of::<cmsghdr>() as u64)
                        .wrapping_add(::core::mem::size_of::<size_t>() as u64)
                        .wrapping_sub(1 as i32 as u64)
                        & !(::core::mem::size_of::<size_t>() as u64).wrapping_sub(1 as i32 as u64),
                ),
            )
            .wrapping_div(::core::mem::size_of::<i32>() as u64) as i32
            >= (unsafe { getdtablesize() })
        {
            (unsafe { *__errno_location() = 11 as i32 });
            (unsafe { free(ifd as *mut libc::c_void) });
            return -(1 as i32) as ssize_t;
        }
        n = unsafe { recvmsg((*imsgbuf).fd, &mut msg, 0 as i32) };
        if n == -(1 as i32) as i64 {
            if (unsafe { *__errno_location() }) != 4 as i32 {
                break;
            }
        } else {
            (unsafe {
                (*imsgbuf).r.wpos =
                    ((*imsgbuf).r.wpos as u64).wrapping_add(n as u64) as size_t as size_t
            });
            cmsg = if msg.msg_controllen >= ::core::mem::size_of::<cmsghdr>() as u64 {
                msg.msg_control as *mut cmsghdr
            } else {
                std::ptr::null_mut::<cmsghdr>()
            };
            while !cmsg.is_null() {
                if (unsafe { (*cmsg).cmsg_level }) == 1 as i32
                    && (unsafe { (*cmsg).cmsg_type }) == SCM_RIGHTS as i32
                {
                    let mut i: i32 = 0;
                    let mut j: i32 = 0;
                    j = ((unsafe {
                        (cmsg as *mut i8)
                            .offset((*cmsg).cmsg_len as isize)
                            .offset_from(((*cmsg).__cmsg_data).as_mut_ptr() as *mut i8)
                    }) as i64 as u64)
                        .wrapping_div(::core::mem::size_of::<i32>() as u64)
                        as i32;
                    i = 0 as i32;
                    while i < j {
                        fd = unsafe {
                            *(((*cmsg).__cmsg_data).as_mut_ptr() as *mut i32).offset(i as isize)
                        };
                        if !ifd.is_null() {
                            (unsafe { (*ifd).fd = fd });
                            (unsafe { (*ifd).entry.tqe_next = std::ptr::null_mut::<imsg_fd>() });
                            (unsafe { (*ifd).entry.tqe_prev = (*imsgbuf).fds.tqh_last });
                            (unsafe { *(*imsgbuf).fds.tqh_last = ifd });
                            (unsafe { (*imsgbuf).fds.tqh_last = &mut (*ifd).entry.tqe_next });
                            ifd = std::ptr::null_mut::<imsg_fd>();
                        } else {
                            (unsafe { close(fd) });
                        }
                        i += 1;
                    }
                }
                cmsg = unsafe { __cmsg_nxthdr(&mut msg, cmsg) };
            }
            break;
        }
    }
    (unsafe { free(ifd as *mut libc::c_void) });
    return n;
}
#[no_mangle]
pub extern "C" fn imsg_get(mut imsgbuf: *mut imsgbuf, mut imsg: *mut imsg) -> ssize_t {
    let mut m: imsg = imsg {
        hdr: imsg_hdr {
            type_0: 0,
            len: 0,
            flags: 0,
            peerid: 0,
            pid: 0,
        },
        fd: 0,
        data: std::ptr::null_mut::<libc::c_void>(),
        buf: std::ptr::null_mut::<ibuf>(),
    };
    let mut av: size_t = 0;
    let mut left: size_t = 0;
    let mut datalen: size_t = 0;
    av = unsafe { (*imsgbuf).r.wpos };
    if ::core::mem::size_of::<imsg_hdr>() as u64 > av {
        return 0 as i32 as ssize_t;
    }
    (unsafe {
        memcpy(
            &mut m.hdr as *mut imsg_hdr as *mut libc::c_void,
            ((*imsgbuf).r.buf).as_mut_ptr() as *const libc::c_void,
            ::core::mem::size_of::<imsg_hdr>() as u64,
        )
    });
    if (m.hdr.len as u64) < ::core::mem::size_of::<imsg_hdr>() as u64
        || m.hdr.len as i32 > 16384 as i32
    {
        (unsafe { *__errno_location() = 34 as i32 });
        return -(1 as i32) as ssize_t;
    }
    if m.hdr.len as u64 > av {
        return 0 as i32 as ssize_t;
    }
    m.fd = -(1 as i32);
    m.buf = std::ptr::null_mut::<ibuf>();
    m.data = std::ptr::null_mut::<libc::c_void>();
    datalen = (m.hdr.len as u64).wrapping_sub(::core::mem::size_of::<imsg_hdr>() as u64);
    (unsafe {
        (*imsgbuf).r.rptr = ((*imsgbuf).r.buf)
            .as_mut_ptr()
            .offset(::core::mem::size_of::<imsg_hdr>() as u64 as isize)
    });
    if datalen != 0 as i32 as u64 {
        m.buf = unsafe { ibuf_open(datalen) };
        if (m.buf).is_null() {
            return -(1 as i32) as ssize_t;
        }
        if (unsafe { ibuf_add(m.buf, (*imsgbuf).r.rptr as *const libc::c_void, datalen) })
            == -(1 as i32)
        {
            (unsafe { ibuf_free(m.buf) });
            return -(1 as i32) as ssize_t;
        }
        m.data = unsafe { ibuf_data(m.buf) };
    }
    if m.hdr.flags as i32 & 1 as i32 != 0 {
        m.fd = imsg_dequeue_fd(imsgbuf);
    }
    if (m.hdr.len as u64) < av {
        left = av.wrapping_sub(m.hdr.len as u64);
        (unsafe {
            memmove(
                &mut (*imsgbuf).r.buf as *mut [u8; 65535] as *mut libc::c_void,
                ((*imsgbuf).r.buf)
                    .as_mut_ptr()
                    .offset(m.hdr.len as i32 as isize) as *const libc::c_void,
                left,
            )
        });
        (unsafe { (*imsgbuf).r.wpos = left });
    } else {
        (unsafe { (*imsgbuf).r.wpos = 0 as i32 as size_t });
    }
    (unsafe { *imsg = m });
    return datalen.wrapping_add(::core::mem::size_of::<imsg_hdr>() as u64) as ssize_t;
}
#[no_mangle]
pub extern "C" fn imsg_get_ibuf(mut imsg: *mut imsg, mut ibuf: *mut ibuf) -> i32 {
    if (unsafe { (*imsg).buf }).is_null() {
        (unsafe { *__errno_location() = 74 as i32 });
        return -(1 as i32);
    }
    return unsafe { ibuf_get_ibuf((*imsg).buf, ibuf_size((*imsg).buf), ibuf) };
}
#[no_mangle]
pub extern "C" fn imsg_get_data(
    mut imsg: *mut imsg,
    mut data: *mut libc::c_void,
    mut len: size_t,
) -> i32 {
    if len == 0 as i32 as u64 {
        (unsafe { *__errno_location() = 22 as i32 });
        return -(1 as i32);
    }
    if (unsafe { (*imsg).buf }).is_null() || (unsafe { ibuf_size((*imsg).buf) }) != len {
        (unsafe { *__errno_location() = 74 as i32 });
        return -(1 as i32);
    }
    return unsafe { ibuf_get((*imsg).buf, data, len) };
}
#[no_mangle]
pub extern "C" fn imsg_get_fd(mut imsg: *mut imsg) -> i32 {
    let mut fd: i32 = unsafe { (*imsg).fd };
    (unsafe { (*imsg).fd = -(1 as i32) });
    return fd;
}
#[no_mangle]
pub extern "C" fn imsg_get_id(mut imsg: *mut imsg) -> uint32_t {
    return unsafe { (*imsg).hdr.peerid };
}
#[no_mangle]
pub extern "C" fn imsg_get_len(mut imsg: *mut imsg) -> size_t {
    if (unsafe { (*imsg).buf }).is_null() {
        return 0 as i32 as size_t;
    }
    return unsafe { ibuf_size((*imsg).buf) };
}
#[no_mangle]
pub extern "C" fn imsg_get_pid(mut imsg: *mut imsg) -> pid_t {
    return (unsafe { (*imsg).hdr.pid }) as pid_t;
}
#[no_mangle]
pub extern "C" fn imsg_get_type(mut imsg: *mut imsg) -> uint32_t {
    return unsafe { (*imsg).hdr.type_0 };
}
#[no_mangle]
pub extern "C" fn imsg_compose(
    mut imsgbuf: *mut imsgbuf,
    mut type_0: uint32_t,
    mut id: uint32_t,
    mut pid: pid_t,
    mut fd: i32,
    mut data: *const libc::c_void,
    mut datalen: size_t,
) -> i32 {
    let mut wbuf: *mut ibuf = std::ptr::null_mut::<ibuf>();
    wbuf = imsg_create(imsgbuf, type_0, id, pid, datalen);
    if wbuf.is_null() {
        return -(1 as i32);
    }
    if imsg_add(wbuf, data, datalen) == -(1 as i32) {
        return -(1 as i32);
    }
    (unsafe { ibuf_fd_set(wbuf, fd) });
    imsg_close(imsgbuf, wbuf);
    return 1 as i32;
}
#[no_mangle]
pub extern "C" fn imsg_composev(
    mut imsgbuf: *mut imsgbuf,
    mut type_0: uint32_t,
    mut id: uint32_t,
    mut pid: pid_t,
    mut fd: i32,
    mut iov: *const iovec,
    mut iovcnt: i32,
) -> i32 {
    let mut wbuf: *mut ibuf = std::ptr::null_mut::<ibuf>();
    let mut i: i32 = 0;
    let mut datalen: size_t = 0 as i32 as size_t;
    i = 0 as i32;
    while i < iovcnt {
        datalen = (datalen as u64).wrapping_add(unsafe { (*iov.offset(i as isize)).iov_len })
            as size_t as size_t;
        i += 1;
    }
    wbuf = imsg_create(imsgbuf, type_0, id, pid, datalen);
    if wbuf.is_null() {
        return -(1 as i32);
    }
    i = 0 as i32;
    while i < iovcnt {
        if imsg_add(
            wbuf,
            unsafe { (*iov.offset(i as isize)).iov_base },
            unsafe { (*iov.offset(i as isize)).iov_len },
        ) == -(1 as i32)
        {
            return -(1 as i32);
        }
        i += 1;
    }
    (unsafe { ibuf_fd_set(wbuf, fd) });
    imsg_close(imsgbuf, wbuf);
    return 1 as i32;
}
#[no_mangle]
pub extern "C" fn imsg_compose_ibuf(
    mut imsgbuf: *mut imsgbuf,
    mut type_0: uint32_t,
    mut id: uint32_t,
    mut pid: pid_t,
    mut buf: *mut ibuf,
) -> i32 {
    let mut hdrbuf: *mut ibuf = std::ptr::null_mut::<ibuf>();
    let mut hdr: imsg_hdr = imsg_hdr {
        type_0: 0,
        len: 0,
        flags: 0,
        peerid: 0,
        pid: 0,
    };
    let mut save_errno: i32 = 0;
    if (unsafe { ibuf_size(buf) }).wrapping_add(::core::mem::size_of::<imsg_hdr>() as u64)
        > 16384 as i32 as u64
    {
        (unsafe { *__errno_location() = 34 as i32 });
    } else {
        hdr.type_0 = type_0;
        hdr.len = (unsafe { ibuf_size(buf) })
            .wrapping_add(::core::mem::size_of::<imsg_hdr>() as u64) as uint16_t;
        hdr.flags = 0 as i32 as uint16_t;
        hdr.peerid = id;
        hdr.pid = pid as uint32_t;
        if hdr.pid == 0 as i32 as u32 {
            hdr.pid = (unsafe { (*imsgbuf).pid }) as uint32_t;
        }
        hdrbuf = unsafe { ibuf_open(::core::mem::size_of::<imsg_hdr>() as u64) };
        if !hdrbuf.is_null() {
            if imsg_add(
                hdrbuf,
                &mut hdr as *mut imsg_hdr as *const libc::c_void,
                ::core::mem::size_of::<imsg_hdr>() as u64,
            ) != -(1 as i32)
            {
                (unsafe { ibuf_close(&mut (*imsgbuf).w, hdrbuf) });
                (unsafe { ibuf_close(&mut (*imsgbuf).w, buf) });
                return 1 as i32;
            }
        }
    }
    save_errno = unsafe { *__errno_location() };
    (unsafe { ibuf_free(buf) });
    (unsafe { ibuf_free(hdrbuf) });
    (unsafe { *__errno_location() = save_errno });
    return -(1 as i32);
}
#[no_mangle]
pub extern "C" fn imsg_forward(mut imsgbuf: *mut imsgbuf, mut msg: *mut imsg) -> i32 {
    let mut wbuf: *mut ibuf = std::ptr::null_mut::<ibuf>();
    let mut len: size_t = 0 as i32 as size_t;
    if (unsafe { (*msg).fd }) != -(1 as i32) {
        (unsafe { close((*msg).fd) });
        (unsafe { (*msg).fd = -(1 as i32) });
    }
    if !(unsafe { (*msg).buf }).is_null() {
        (unsafe { ibuf_rewind((*msg).buf) });
        len = unsafe { ibuf_size((*msg).buf) };
    }
    wbuf = imsg_create(
        imsgbuf,
        unsafe { (*msg).hdr.type_0 },
        unsafe { (*msg).hdr.peerid },
        (unsafe { (*msg).hdr.pid }) as pid_t,
        len,
    );
    if wbuf.is_null() {
        return -(1 as i32);
    }
    if !(unsafe { (*msg).buf }).is_null() {
        if (unsafe { ibuf_add_buf(wbuf, (*msg).buf) }) == -(1 as i32) {
            (unsafe { ibuf_free(wbuf) });
            return -(1 as i32);
        }
    }
    imsg_close(imsgbuf, wbuf);
    return 1 as i32;
}
#[no_mangle]
pub extern "C" fn imsg_create(
    mut imsgbuf: *mut imsgbuf,
    mut type_0: uint32_t,
    mut id: uint32_t,
    mut pid: pid_t,
    mut datalen: size_t,
) -> *mut ibuf {
    let mut wbuf: *mut ibuf = std::ptr::null_mut::<ibuf>();
    let mut hdr: imsg_hdr = imsg_hdr {
        type_0: 0,
        len: 0,
        flags: 0,
        peerid: 0,
        pid: 0,
    };
    datalen = (datalen as u64).wrapping_add(::core::mem::size_of::<imsg_hdr>() as u64) as size_t
        as size_t;
    if datalen > 16384 as i32 as u64 {
        (unsafe { *__errno_location() = 34 as i32 });
        return std::ptr::null_mut::<ibuf>();
    }
    hdr.type_0 = type_0;
    hdr.flags = 0 as i32 as uint16_t;
    hdr.peerid = id;
    hdr.pid = pid as uint32_t;
    if hdr.pid == 0 as i32 as u32 {
        hdr.pid = (unsafe { (*imsgbuf).pid }) as uint32_t;
    }
    wbuf = unsafe { ibuf_dynamic(datalen, 16384 as i32 as size_t) };
    if wbuf.is_null() {
        return std::ptr::null_mut::<ibuf>();
    }
    if imsg_add(
        wbuf,
        &mut hdr as *mut imsg_hdr as *const libc::c_void,
        ::core::mem::size_of::<imsg_hdr>() as u64,
    ) == -(1 as i32)
    {
        return std::ptr::null_mut::<ibuf>();
    }
    return wbuf;
}
#[no_mangle]
pub extern "C" fn imsg_add(
    mut msg: *mut ibuf,
    mut data: *const libc::c_void,
    mut datalen: size_t,
) -> i32 {
    if datalen != 0 {
        if (unsafe { ibuf_add(msg, data, datalen) }) == -(1 as i32) {
            (unsafe { ibuf_free(msg) });
            return -(1 as i32);
        }
    }
    return datalen as i32;
}
#[no_mangle]
pub extern "C" fn imsg_close(mut imsgbuf: *mut imsgbuf, mut msg: *mut ibuf) {
    let mut hdr: *mut imsg_hdr = std::ptr::null_mut::<imsg_hdr>();
    hdr = (unsafe { (*msg).buf }) as *mut imsg_hdr;
    (unsafe { (*hdr).flags = ((*hdr).flags as i32 & !(1 as i32)) as uint16_t });
    if (unsafe { ibuf_fd_avail(msg) }) != 0 {
        (unsafe { (*hdr).flags = ((*hdr).flags as i32 | 1 as i32) as uint16_t });
    }
    (unsafe { (*hdr).len = ibuf_size(msg) as uint16_t });
    (unsafe { ibuf_close(&mut (*imsgbuf).w, msg) });
}
#[no_mangle]
pub extern "C" fn imsg_free(mut imsg: *mut imsg) {
    (unsafe { ibuf_free((*imsg).buf) });
}
extern "C" fn imsg_dequeue_fd(mut imsgbuf: *mut imsgbuf) -> i32 {
    let mut fd: i32 = 0;
    let mut ifd: *mut imsg_fd = std::ptr::null_mut::<imsg_fd>();
    ifd = unsafe { (*imsgbuf).fds.tqh_first };
    if ifd.is_null() {
        return -(1 as i32);
    }
    fd = unsafe { (*ifd).fd };
    if !(unsafe { (*ifd).entry.tqe_next }).is_null() {
        (unsafe { (*(*ifd).entry.tqe_next).entry.tqe_prev = (*ifd).entry.tqe_prev });
    } else {
        (unsafe { (*imsgbuf).fds.tqh_last = (*ifd).entry.tqe_prev });
    }
    (unsafe { *(*ifd).entry.tqe_prev = (*ifd).entry.tqe_next });
    (unsafe { free(ifd as *mut libc::c_void) });
    return fd;
}
#[no_mangle]
pub extern "C" fn imsg_flush(mut imsgbuf: *mut imsgbuf) -> i32 {
    while (unsafe { (*imsgbuf).w.queued }) != 0 {
        if (unsafe { msgbuf_write(&mut (*imsgbuf).w) }) <= 0 as i32 {
            return -(1 as i32);
        }
    }
    return 0 as i32;
}
#[no_mangle]
pub extern "C" fn imsg_clear(mut imsgbuf: *mut imsgbuf) {
    let mut fd: i32 = 0;
    (unsafe { msgbuf_clear(&mut (*imsgbuf).w) });
    loop {
        fd = imsg_dequeue_fd(imsgbuf);
        if fd == -(1 as i32) {
            break;
        }
        (unsafe { close(fd) });
    }
}
